/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.screen.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boxin.ims.modules.wechat.entity.screen.BlessWords;
import com.boxin.ims.modules.wechat.entity.screen.JsonScreenResult;
import com.boxin.ims.modules.wechat.entity.screen.JsonScreenWords;
import com.boxin.ims.modules.wechat.entity.screen.WeddingScreen;
import com.boxin.ims.modules.wechat.service.screen.BlessWordsService;
import com.boxin.ims.modules.wechat.service.screen.WeddingScreenService;
import com.boxin.ims.modules.wechat.utils.ScreenUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 婚庆微信墙Controller
 * @author Jakemanse
 * @version 2013-11-01
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/wechat/screen/weddingScreen")
public class WeddingScreenFrontController extends BaseController {

	@Autowired
	private WeddingScreenService weddingScreenService;
	
	@Autowired
	private BlessWordsService blessWordsService;
	
	
	@RequestMapping(value = {"", "/{id}"})
	public String screen(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		WeddingScreen screen =  weddingScreenService.get(id);
		
		model.addAttribute("screen", screen);
		return "modules/wechat/screen/front/default/screen";
	}


	@RequestMapping(value = "toScreen",method = RequestMethod.GET)
	public String queryToScreen(HttpServletRequest request, HttpServletResponse response){
		BlessWords blessWords=new BlessWords();
		String srnId = request.getParameter("srnid");		//所属屏
		Long screenId = Long.parseLong((StringUtils.isEmpty(srnId)?"0":srnId));
		
		List<BlessWords> blessWrodsList  =  ScreenUtils.popAllByScreenId(screenId);
		
//		if(blessWrodsList==null || blessWrodsList.isEmpty()){
//			WeddingScreen screen = new WeddingScreen();
//			screen.setId(screenId);
//			
//			blessWords.setScreen(screen);
//			blessWords.setStatus(BlessWords.STATUS_DISPLAY_NO);	//未 显示的
//			blessWrodsList = blessWordsService.find(blessWords);
//		}
//		
		
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		JsonScreenResult jsonResult = new JsonScreenResult();
		
		List<JsonScreenWords> jsList = new ArrayList<JsonScreenWords>();
		if(blessWrodsList==null || blessWrodsList.isEmpty()){	//没有新的祝福词
			jsonMap.put("ret", "1");
		}else{
			
			for(BlessWords bw:blessWrodsList){
				JsonScreenWords jw = new JsonScreenWords();
				jw.setContent(bw.getWords());
				jw.setSend_time(System.currentTimeMillis()+"");
				jw.setWxid(bw.getMember().getWechatId());
				jw.setName(bw.getName());
				jw.setPhoto("");
				jsList.add(jw);
				
			}
			jsonMap.put("ret", "0");
		}
		
		jsonResult.setList(jsList);
		jsonMap.put("data", jsonResult);
		
		responseJson(request, response, jsonMap);
		return null;
	}
	
	
	
	

}
